package easy.Sqrt;

/**
 * Sqrt
 *
 * @author CodeX
 * @since 2021-09-28 16:16:49
 */
public class MySqrt {
    public static int mySqrt(int x) {
        // 二分查找
        int left = 0, right = x, ans = Integer.MIN_VALUE;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if ((long) mid * mid <= x) {
                ans = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        System.out.println(mySqrt(8));
    }
}
